package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.system.UserBusdRefEntity;
import com.dragon.modules.yuegou.enums.system.UserBusdRefType;
import com.dragon.modules.yuegou.mapper.system.UserBusdRefMapper;
import com.dragon.modules.yuegou.model.system.UserBusdRef;
import com.dragon.modules.yuegou.query.system.UserBusdRefQuery;
import com.dragon.modules.yuegou.service.system.UserBusdRefService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户商圈权限 服务实现
 *
 * @author pbuilder generated
 * @date 2022-09-13 15:43:58
 * @version 2.0
 */
@Service("sysUserBusdRefService")
public class UserBusdRefServiceImpl
		extends ServiceImpl<UserBusdRefMapper, UserBusdRefEntity>
		implements UserBusdRefService {

	@Override
	public void saveOrEditList(String userId, List<String> busdIdList, UserBusdRefType type){
		UserBusdRefQuery userBusdRefQuery = new UserBusdRefQuery();
		userBusdRefQuery.setUserId(userId);
		userBusdRefQuery.setType(type);
		userBusdRefQuery.setDel(false);
		List<UserBusdRefEntity> refList = baseMapper.selectList(userBusdRefQuery.toQueryWrapper());
		if (CollectionUtils.isEmpty(refList)){
			for (String busdId:busdIdList){
				this.saveUserBusdRef(busdId,userId,type);
			}
		}else {
			List<UserBusdRefEntity> noExistList = refList.stream().filter(userBusdRef -> !busdIdList.contains(userBusdRef.getBusdId())).collect(Collectors.toList());
			if (CollectionUtils.isNotEmpty(noExistList)){
				this.delList(noExistList);
			}

			List<String> existList = refList.stream().filter(userBusdRef -> busdIdList.contains(userBusdRef.getBusdId())).map(UserBusdRefEntity::getBusdId).distinct().collect(Collectors.toList());
			//排除存在的
			List<String> insertList = busdIdList.stream().filter(busdId-> !existList.contains(busdId)).collect(Collectors.toList());
			if (CollectionUtils.isNotEmpty(insertList)){
				for (String busdId:insertList){
					this.saveUserBusdRef(busdId,userId,type);
				}
			}
		}
	}

	private void saveUserBusdRef(String busdId,String userId,UserBusdRefType type){
		UserBusdRef userBusdRef = new UserBusdRef();
		userBusdRef.setBusdId(busdId);
		userBusdRef.setDel(false);
		userBusdRef.setUserId(userId);
		userBusdRef.setType(type);
		save(userBusdRef);
	}


	@Override
	public void delByUserId(String userId,UserBusdRefType type){
		UserBusdRefQuery userBusdRefQuery = new UserBusdRefQuery();
		userBusdRefQuery.setUserId(userId);
		userBusdRefQuery.setType(type);
		userBusdRefQuery.setDel(false);
		List<UserBusdRefEntity> refList = baseMapper.selectList(userBusdRefQuery.toQueryWrapper());
		this.delList(refList);

	}
	private void delList(List<UserBusdRefEntity> refList){
		for (UserBusdRefEntity ref:refList){
			this.delById(ref.getId());
		}
	}

	private void delById(String id){
		UserBusdRefEntity entity = new UserBusdRefEntity();
		entity.setDel(true);
		entity.setId(id);
		baseMapper.updateById(entity);
	}
}